//
//  L_BaseViewController.swift
//  suda
//
//  Created by youai on 2019/7/15.
//  Copyright © 2019年 youi. All rights reserved.
//

import Foundation
import UIKit

///抽象的登录模块共同基础页面
class L_BaseViewController : BaseViewController {
    
    let headView = UIImageView()
    let footView = UIImageView()
    ///前一个灰色圆角图形
    var lastRoundCornerView : UIView? = nil
}

// MARK: 界面UI绘制
extension L_BaseViewController {
    
    @objc func setupUI(){
        view.backgroundColor = .white
        //头部图
        safeView.drawImage(imageView:headView, named: "L_Icon") { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(120)
        }
        //底部图
        safeView.drawImage(imageView:footView, named: nil) { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    ///绘制圆角灰色视图，因为该模块有大量宽高一致，纵向排列的圆角灰色视图，所以抽象出来
    ///新的视图将会由headView或者上一个roundCornerView的底部向下偏移offset个点
    func drawRoundCornerView(topOffset:CGFloat,backgroundColor:UIColor = .clear)->UIView{
        let result = UIView()
        safeView.addSubview(result)
        layoutRoundCornerView(roundCornerView: result, offset: topOffset)
        result.backgroundColor = backgroundColor
        return result
    }
    
    func drawImgLineView(topOffset:CGFloat,img:UIImage?,backgroundColor:UIColor = .clear) -> (result: UIView, imageView: UIImageView){
        let result = UIView()
        safeView.addSubview(result)
        layoutRoundCornerView(roundCornerView: result, offset: topOffset)
        result.backgroundColor = backgroundColor
        let imageView = result.drawImage(named: nil) { (make) in
            make.left.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        imageView.image = img
        
        //画横线
        let line = result.drawView { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-1)
            make.height.equalTo(1)
        }
        line.backgroundColor = .xF2F2F2
        
        return (result, imageView)
    }
    
    ///绘制登录模块中的圆角按钮，样式一致
    func drawRoundCornerButton(title:String,topOffset:CGFloat)->Button{
        let result = Button()
        result.setTitleAndColor(title: title, color: .white)
        result.setTitleAndColor(title: title, color: .black,for : .highlighted)
        result.setTitleLabelFont(fontSize: 14, fontWeight: .medium)
        safeView.addSubview(result)
        layoutRoundCornerView(roundCornerView: result, offset: topOffset)
//        result.drawGradientLayer(color1: .xFC4C02, color2: .xFC4C02, x1: 0.5, y1: 0, x2: 0.5, y2: 1.0)
        result.setBackgroundImage(UIImage.createImageFrom(color: .xFC4C02), for: .normal)
        result.setBackgroundImage(UIImage.createImageFrom(color: .xFFC7C7), for: .selected)
        result.layer.cornerRadius = 5
        result.clipsToBounds = true
        return result
    }
    
    ///在圆角视图内绘制输入框，样式一致
    func drawTextFieldInRoundCornerView(textField:TextField,placeHolder:String,
                                        roundCornerView:UIView,leftOffset : CGFloat = 25,rightOffset : CGFloat = -20){
        roundCornerView.drawTextField(textField: textField, placeholder: placeHolder, fontSize: 13, fontWeight: .medium, keyboardType: .default, returnKeyType: .done) { (make) in
            make.left.equalToSuperview().offset(leftOffset)
            make.right.equalToSuperview().offset(rightOffset)
            make.centerY.equalToSuperview()
        }
        textField.hitEdgInsets = .init(top: -20, left: -20, bottom: -20, right: -20)
    }
    
    ///对圆角视图进行布局
    private func layoutRoundCornerView(roundCornerView:UIView,offset:CGFloat){
        roundCornerView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.equalTo(282)
            make.height.equalTo(37)
            make.top.equalTo((lastRoundCornerView ?? headView).snp.bottom).offset(offset)
        }
        roundCornerView.cornerRadius(5)
        lastRoundCornerView = roundCornerView
    }
    
    func drawRememberCheck(button:Button, title:String="  记住登录密码"){
        safeView.drawButton(button:button, title: title, titleColor: .x999999, fontSize: 13,fontWeight: .medium) { (make) in
            //固定间距12
            make.top.equalTo(lastRoundCornerView!.snp.bottom).offset(12)
            make.right.equalTo(lastRoundCornerView!).offset(-11)
        }
        button.setImage(UIImage(named: "login_notchecked"), for: .normal)
        button.setImage(UIImage(named: "login_checked"), for: .selected)
        button.hitEdgInsets = .init(top: -5, left: -20, bottom: -10, right: -20)
    }
}

